import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ytbusiness/config/yt_theme.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';
import 'package:ytbusiness/widgets/widgets.dart';

///选择经营类目
class SelectManageCategory extends StatefulWidget {
  ///type:   1/经营类目2/主机品牌3/配件品牌
  ///title:""
  final Map<String,dynamic> arguments;

  const SelectManageCategory({Key key, this.arguments}) : super(key: key);
  @override
  _SelectManageCategoryState createState() => _SelectManageCategoryState();
}

class _SelectManageCategoryState extends State<SelectManageCategory> {
  List<int> _checkList = []; //选中经营类目
  List<String> title = ["奥迪","宝马","奔驰","劳斯莱斯"];


  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        //输入框失焦
        FocusScope.of(context).requestFocus(FocusNode());
      },
      child: Scaffold(
          backgroundColor: Colors.white,
          appBar: customAppBar(context,title: "${widget.arguments["title"]}"),
          body: Column(
            children: [
              Container(
                margin: EdgeInsets.only(
                    left: setWidth(15), right: setWidth(15), top: setWidth(15)),
                height: setWidth(36),
                decoration: BoxDecoration(
                    color: Color(0xffF9F9F9),
                    borderRadius: BorderRadius.circular(18)),
                child: TextField(
                  maxLines: 1,
                  keyboardType: TextInputType.multiline,
                  autofocus: false,
                  style:
                      TextStyle(color: Color(0xff030303), fontSize: setSp(16)),
                  cursorColor: YTTheme.theme,
                  textAlign: TextAlign.start,
                  enableInteractiveSelection: false,
                  inputFormatters: [
                    WhitelistingTextInputFormatter(
                        RegExp("[a-zA-Z]|[0-9.-_]|[\u4e00-\u9fa5]")),
                  ],
                  decoration: InputDecoration(
                      contentPadding: EdgeInsets.only(left: setWidth(19)),
                      border: InputBorder.none,
                      focusedBorder: InputBorder.none,
                      enabledBorder: InputBorder.none,
                      prefixIcon: Container(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            GestureDetector(
                              behavior: HitTestBehavior.opaque,
                              onTap: () {},
                              child: Container(
                                  width: setWidth(30),
                                  alignment: Alignment.center,
                                  child: Column(
                                    children: [
                                      Container(
                                        width: setWidth(18),
                                        height: setWidth(19),
                                        child: Image.asset(
                                          "assets/images/search.png",
                                          fit: BoxFit.cover,
                                          color: Color(0xff989898),
                                        ),
                                      ),
                                    ],
                                  )),
                            )
                          ],
                        ),
                      ),
                      hintText: '输入搜索${widget.arguments["title"]}',
                      hintStyle: TextStyle(
                          color: Color(0xff989898), fontSize: setSp(15))),
                ),
              ),
              SizedBox(
                height: setWidth(10),
              ),
              GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: (){
                  setState(() {
                    if(_checkList.length == title.length){
                      _checkList.clear();
                    }else{
                      for(int i = 0; i < title.length; i++){
                        if (_checkList.indexOf(i) == -1) {
                          _checkList.add(i);
                        }
                      }
                    }

                  });


                },
                child: Container(
                  decoration: BoxDecoration(
                      border: Border(
                        bottom: BorderSide(
                          color: Color(0xffF0F0F0),
                          width: 0.5,),
                      )
                  ),
                  padding: EdgeInsets.all(setWidth(15)),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Container(
                        child: Text(_checkList.length != title.length?"全选":"取消",style: TextStyle(color: Color(0xff321212),fontSize: setSp(16)),),
                      ),//
                      Container(
                        padding: EdgeInsets.only(left: setWidth(3),right: setWidth(3)),
                        child:Image.asset("assets/images/check_pay.png",fit: BoxFit.cover,color:_checkList.length != title.length?Color(0xff989898): YTTheme.theme,),
                        width: setWidth(23),
                        height: setWidth(17),
                      ),// qs.png
                    ],
                  ),
                ),
              ),
              Expanded(
                  child: Container(
                    child: ListView.builder(
                        itemCount: title.length,
                        shrinkWrap: true,
                        itemBuilder: (context,index){
                      return GestureDetector(
                          onTap: () {
                            setState(() {
                              if (_checkList.indexOf(index) != -1) {
                                _checkList.remove(index);
                              } else {
                                _checkList.add(index);
                              }
                            });
                          },
                          child: widget.arguments["type"] == "1"?
                          _item(index: index,check: _checkList):
                              _imageItem(index: index,check: _checkList),
                      
                      );
                    }),


              )),
              SizedBox(
                height: setWidth(70),
              ),
            ],
          ),
          //提交
          bottomSheet: confirmButton(title: "确定", voidCallback: () {
            List<String> checkTitle = [];
            _checkList.forEach((element) {
              checkTitle.add(title[element]);
            });


            Navigator.pop(context,checkTitle);

          })),
    );
  }
  //无图item
  Widget _item({int index,List<int> check}) {

    return Container(
      decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              color: Color(0xffF0F0F0),
              width: 0.5,),
          )
      ),
      padding: EdgeInsets.only(left: setWidth(15),top: setWidth(23),right: setWidth(15),bottom: setWidth(23)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Container(
            child: Text(title[index],style: TextStyle(color: Color(0xff321212),fontSize: setSp(16)),),
          ),//
          check.indexOf(index) == -1 ?Container():Container(
            child: Image.asset("assets/images/check_pay.png",fit: BoxFit.cover,color: YTTheme.theme,),
            width: setWidth(17),
            height: setWidth(17),
          )
        ],
      ),
    );
  }
  //有图item
  Container _imageItem({int index, List<int> check}) {
    return Container(
      decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              color: Color(0xffF0F0F0),
              width: 0.5,),
          )
      ),
      padding: EdgeInsets.only(left: setWidth(15),top: setWidth(23),right: setWidth(15),bottom: setWidth(23)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Container(
                height: setWidth(48),
                width: setWidth(48),
                decoration: BoxDecoration(
                  color: Colors.black,
                  borderRadius: BorderRadius.circular(25)
                ),
              ),
              SizedBox(
                width: setWidth(12),
              ),
              Container(
                child: Text(title[index],style: TextStyle(color: Color(0xff321212),fontSize: setSp(16)),),
              ),//
            ],
          ),
          check.indexOf(index) == -1 ?Container():Container(
            child: Image.asset("assets/images/check_pay.png",fit: BoxFit.cover,color: YTTheme.theme,),
            width: setWidth(17),
            height: setWidth(17),
          )
        ],
      ),
    );
    
  }


}
